package com.xinguan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinguan.entity.Users;
import com.xinguan.mapper.UserMapper;
import com.xinguan.pojo.dto.ChangePasswordDto;
import com.xinguan.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 陈俊行
 * @Date: 2023/11/04/19:56
 * @Description:
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UserMapper, Users> implements UsersService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public void updatePassword(ChangePasswordDto changePasswordDto) {
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getId,changePasswordDto.getId());
        Users users = userMapper.selectOne(wrapper);
        users.setPassword(changePasswordDto.getModifiedpassword());
        userMapper.updateById(users);

    }

    @Override
    public void deleteAdmin(String id) {
        LambdaQueryWrapper<Users> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(Users::getId,id);
        Users users = userMapper.selectOne(wrapper);
        users.setDeleted(1);
        userMapper.updateById(users);
//        userMapper.deleteById(users);
    }

    @Override
    public PageInfo<Users> getEmployeePage(Integer current, Integer pageSize) {
        //开启分页功能，每条显示pageSize条数
        PageHelper.startPage(current,pageSize);
        //查询所有的员工信息，并根据account进行排序
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getDeleted, 0)
                .orderByAsc(Users::getAccount);
        List<Users> users = userMapper.selectList(wrapper);
        //获取分页相关数据，显示当前页前后各5个页码
        PageInfo<Users> page = new PageInfo<>(users,5);
        return  page;

    }

    @Override
    public Map<String, Object> getUsers(String id) {
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getId,id);
        Users users = userMapper.selectOne(wrapper);
        Map<String,Object> data = new HashMap<>();
        data.put("adminaccount",users.getAccount());
        data.put("name",users.getName());
        return data;
    }



}
